package com.cby.leetcode.哈希.LC1;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: LC1
 * Package: com.cby.leetcode.哈希.LC1
 * Description:
 *
 * @Author dhu-bion
 * @Create 2025/9/4 21:58
 * @Version 1.0
 */
public class LC1 {

    public static void main(String args[]){
        Solution solution = new Solution();
        int[] nums = {2,7,11,15};

        int[] res = solution.twoSum(nums, 9);
        Arrays.stream(res).forEach(System.out::println);
    }

    static class Solution{
        public int[] twoSum(int[] nums,int target){
            //键为数组值，值为数组下标
            Map<Integer,Integer> idx = new HashMap<>();
            for(int j = 0 ; ; j++){
                if(idx.containsKey(target - nums[j])){
                    //找到
                    return new int[] {idx.get(target - nums[j]),j};
                }
                


                //存入
                idx.put(nums[j],j);
            }

        }
    }


}
